{#-
  Device specific sizes and addresses. These variables describes the physical
  memory of the device.
#}
{%- set sram_addr = device_ram_addr | first | default(536870912, true) %}
{%- set sram_size = device_ram_size | first %}
{%- set flash_addr = device_flash_addr | first | default(0, true) %}
{%- set flash_size = device_flash_size | first %}
{%- set flash_page_size = device_flash_page_size | first %}
{%- set bootloader_flash_addr = '0xFE10000' | int(base=16) %}
{%- set bootloader_flash_size = device_bootloader_flash_size | sum %}

{#-
  Application specific sizes. Here we start to calculate the application view
  of the physical memory.
#}
{%- set app_flash_start = flash_addr %}
{%- set app_flash_end = flash_addr + flash_size %}
{%- set app_ram_start = sram_addr %}
{%- set app_ram_end = sram_addr + sram_size %}

{#-
  On series-2 devices we reserve the last flash page for tokens and key storage
#}
{%- if (device_series | first) == 2 %}
{%-   set app_flash_end = app_flash_end - flash_page_size %}
{%- endif %}

{#-
  Reserve space for the bootloader at the beginning of flash. Note that
  bootloader_size > 0 only for devices that place the bootloader in main flash.
  On devices where the bootloader is placed in the dedicated bootloader flash
  the bootloader_size value is 0.
#}
{%- set app_bootloader_size = bootloader_size | sum %}
{%- if app_bootloader_size > 0 %}
{%-   set app_flash_start = app_flash_start + app_bootloader_size %}
{%- endif %}

{#-
  Calculate application flash and ram size based on start and end address.
#}
{%- set app_flash_size = app_flash_end - app_flash_start %}
{%- set app_ram_size = app_ram_end - app_ram_start %}

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/* Version 4.1.1 */

/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x{{ '%0x' % app_flash_start }};
define symbol __ICFEDIT_region_ROM_end__   = (0x{{ '%0x' % app_flash_start }}+0x{{ '%0x' % app_flash_size }}-1);
define symbol __ICFEDIT_region_RAM_start__ = 0x{{ '%0x' % app_ram_start }};
define symbol __ICFEDIT_region_RAM_end__   = (0x{{ '%0x' % app_ram_start }}+0x{{ '%0x' % app_ram_size }}-1);

/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK with alignment = 8
{
  section .stack
};

define block HEAP with alignment = 8
{
  section .heap
};

define block header with alignment = 8
{
  section AAT
};

{%- if apploader_enable %}
define block apploader with alignment = {{ flash_page_size }}
{
  readonly section .binapploader
};
{%- endif %}

define block nvm with alignment = {{ flash_page_size }}
{
  section SIMEE,
};
keep { block nvm };

define block storage with alignment = {{ flash_page_size }}
{
  section INTERNAL_STORAGE,
};
keep { block storage };

define block application with fixed order
{
  block header,
  readonly section .intvec,
  readonly
};

define block application_ram with fixed order
{
  block CSTACK,
  readwrite,
  block HEAP
};

initialize by copy { readwrite };

do not initialize
{
 section .noinit,
 section .stack,
 section .heap
};

keep {
  section .intvec,
  section AAT,
  block header
};

{%- if apploader_enable %}
".text_apploader":
place at start of ROM_region  { block apploader };
"application":
place in ROM_region { block application };
{%- else %}
"application":
place at start of ROM_region { block application };
{%- endif %}

"storage_regions":
place at end of ROM_region {
  block nvm,
  block storage
};

"application_ram":
place at start of RAM_region { block application_ram };
